'use strict';
const semver = require('semver')
const colors = require('colors/safe')
const log = require('@zmz-cli-dev/log')
const LOWEST_NODE_VERSION = '16.0.0' // node支持最低版本

class Command {
  constructor(argv) {
    if(!argv){
      throw new Error(colors.red(`参数不能为空`))
    }
    if (!Array.isArray(argv)) {
      throw new Error(colors.red(`参数必须为数组`))
    }
    if (!argv.length) {
      throw new Error(colors.red(`参数列表不能为空`))
    }
    this._argv = argv;
    let runner = new Promise((resolve, reject) => {
      let chain = Promise.resolve();
      chain = chain.then(() => this.checkNodeVersion());
      chain = chain.then(() => this.initArgs());
      chain = chain.then(() => this.init());
      chain = chain.then(() => this.exec());
      chain.catch((err) => {
        log.error(err.message);
      })
    })
  }
  // 检查node版本
  checkNodeVersion() {
    const currentVersion = process.version
    const lowestVersion = LOWEST_NODE_VERSION
    if (!semver.gte(currentVersion, lowestVersion)) {
      throw new Error(colors.red(`zmz-cli需要安装${lowestVersion}以上版本的Node.js`))
    }
  }
  // 初始化参数
  initArgs() {
    this._cmd = this._argv[this._argv.length - 1]
    this._argv = this._argv.slice(0, this._argv.length - 1)
  }

  init() {
    throw new Error('init方法必须被实现')
  }

  exec() {
    throw new Error('exec方法必须被实现')
  }
}
module.exports = Command;
